import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:yd_eval/entity/yd_eval_entity.dart';
import 'package:yd_eval/entity/yd_eval_result_entity.dart';
import 'package:yd_eval/manager/yd_eval_manager.dart';
import 'package:yd_eval/utils/eval_result_utils.dart';
import 'package:yd_eval/yd_eval.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _ydEvalPlugin = YdEval();

  List<InlineSpan>? _widgets;

  @override
  void initState() {
    super.initState();
    initPlatformState();
    _onEvalListener();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await _ydEvalPlugin.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  void _onEvalListener() {
    YdEvalManager.instance.setResultListener((result) {
      switch (result.type) {
        case YDEvalEventType.startRecord:
          debugPrint("开始录音,请讲话");
          break;

        case YDEvalEventType.endRecord:
          break;

        case YDEvalEventType.evalResult:
          debugPrint("评测完成！");
          // var pronunciation = result.resultEntity?.result?.pronunciation;
          List<dynamic> list = EvalResultUtils.instance
              .parseResult(result.resultEntity, result.originText, result.evalType);
          var pronunciation = list[0];
          _widgets = list[1];
          setState(() {

          });
          debugPrint("当前音准：$pronunciation , ${_widgets}");
          break;
        case YDEvalEventType.evalProgress:
          break;
        case YDEvalEventType.error:
          break;
        case YDEvalEventType.toast:
          break;
        case YDEvalEventType.timeout:
          break;
        case YDEvalEventType.close:
          break;
      }
      debugPrint("当前类型：${result.type}, 返回值：${result.result}");
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            ElevatedButton(
              onPressed: () {
                YdEvalManager.instance.startEval(YdEvalEntity(
                  content: "\"hello\" word hello",
                  langType: YdEvalType.TYPE_EN,
                ));
              },
              child: Text("开始评测" "你好啊！"),
            ),
            ElevatedButton(
              onPressed: () {
                YdEvalManager.instance.playRecord();
              },
              child: Text("播放录音"),
            ),
            Text.rich(TextSpan(children: _widgets ?? [])),
          ],
        ),
      ),
    );
  }
}
